package com.fireline.beauty.cashier;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;

/**
 * 备注提醒
 * */
public class BztxEdit implements IFunction {
	private Text text;
	/**
	 * @wbp.nonvisual location=27,582
	 */
	private final Dataset dataset = new Dataset(
			"select * from hyxx where hybh=?", SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=93,592
	 */
	private final Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);
	private Shell shell;
	private String hybh;

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		dataset.setKeyColumns(new String[] {});

		BatchItem batchItem = new BatchItem(batch);
		batchItem.setCommand("update hyxx set bz=? where hybh=?");
		batchItem.setName("修改会员备注");
		Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		ToolBar toolBar = new ToolBar(content, SWT.FLAT | SWT.RIGHT);

		ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
		toolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		toolItem.setText("保存");

		ToolItem toolItem_1 = new ToolItem(toolBar, SWT.NONE);
		toolItem_1.setText("打印");

		text = new Text(content, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		setParams();
		setValues();
		return content;
	}

	public void refresh(String hybh) {
		this.hybh = hybh;
		setParams();
		dataset.update();
		if (dataset.getCount() > 0)
			dataset.setSelected(0);
		setValues();
	}

	protected void doSave() {
		setParams();
		batch.execute();
		MessageDialog.openInformation(shell, "提示", "保存成功");
	}

	protected void setParams() {
		dataset.setParam(0, hybh);
		batch.setParam(0, 0, text.getText());
		batch.setParam(0, 1, dataset.getValue("hybh"));
	}

	protected void setValues() {
		text.setText(dataset.getValue("bz", String.class, ""));
	}
}
